﻿/*
** Haaf's Game Engine 1.6
** Copyright (C) 2003-2006, Relish Games
** hge.relishgames.com
**
** hge_tut06 - Creating menus
*/


// Copy the files "menu.wav", "font1.fnt", "font1.png",
// "bg.png" and "cursor.png" from the folder "precompiled"
// to the folder with executable file. Also copy hge.dll
// and bass.dll to the same folder.


import core.hgeall;

import std.c.math;
		hgeGUI				gui;

class  myclass: hgeSceneObj
{
	
	ifHge			hge;
	hgeSprite 		spr;
	hgeFontCN		fnt;
	hgeFontCN		dfnt;  //default font

	HEFFECT				snd;
	HTEXTURE			tex;
	hgeQuad				quad;
	// Pointers to the HGE objects we will use

	HTEXTURE		but;

	this(){
		super("场景1");
		
		hge= gameInfo.hge();
			// Load sound and textures
		quad.tex=gameInfo.res.tex_load( r"res\bg.png");
		tex=gameInfo.res.tex_load( r"res\cursor.png");
		but=gameInfo.res.tex_load(r"res\button.png");
		snd=hge.Effect_Load(r"res\menu.wav");
		
		// Set up the quad we will use for background animation
		quad.blend=BLEND_ALPHABLEND | BLEND_COLORMUL | BLEND_NOZWRITE;

		for(int i=0;i<4;i++)
		{
			// Set up z-coordinate of vertices
			quad.v[i].z=0.5f;
			// Set up color. The format of DWORD col is 0xAARRGGBB
			quad.v[i].col=0xFFFFFFFF;
		}

		quad.v[0].x=0; quad.v[0].y=0; 
		quad.v[1].x=800; quad.v[1].y=0; 
		quad.v[2].x=800; quad.v[2].y=600; 
		quad.v[3].x=0; quad.v[3].y=600; 


		// Load the font, create the cursor sprite

		fnt =new hgeFontCN(r"res\font_cn.sbin");
		dfnt= new hgeFontCN(fnt);
		//fnt.SetScale(3);
		
		spr=new hgeSprite(tex,0,0,32,32);	mes(" sprite==============");

		// Create and initialize the GUI
		gui=new hgeGUI();
		

		//fnt.SetScale(1.5);
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIButton(1,400,200,64,30,but));
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIButton(2,400,240,64,30,but));
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIButton(3,400,280,64,30,but));
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIButton(4,400,320,64,30,but));
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIButton(5,400,360,64,30,but));



		gui.SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		gui.SetCursor(spr);
		gui.SetFocus(1);
		gui.Enter();
	}

	~this(){
		mes("eixt===============================================");
		hge.Effect_Free(snd);
		//delete gui ;
		delete spr ;
		fnt=null;
	}


	bool update()
	{
		float dt=hge.Timer_GetDelta();
		static float t=0.0f;
		float tx,ty;
		int id;
		static int lastid=0;

		// If ESCAPE was pressed, tell the GUI to finish
		if(hge.Input_GetKeyState(HGEK_ESCAPE)) { lastid=5; gui.Leave(); }
		
		// We update the GUI and take an action if
		// one of the menu items was selected
		id=gui.Update(dt);
		if(id == -1)
		{
			switch(lastid)
			{
				case 1: gui.Enter(); break;
				case 2: gui.Enter(); this.To("场景2"); break;	
				case 3:	 gui.Enter(); return true;
				case 4:
				case 6:
					//gui.SetFocus(1);
				case 5: gui.Enter();
				default: 
					break;
			}
		}
		else if(id) { lastid=id; gui.Leave(); }

		// Here we update our background animation
		t+=dt;
		tx=50*cosf(t/60);
		ty=50*sinf(t/60);

		quad.v[0].tx=tx;        quad.v[0].ty=ty;
		quad.v[1].tx=tx+800/64; quad.v[1].ty=ty;
		quad.v[2].tx=tx+800/64; quad.v[2].ty=ty+600/64;
		quad.v[3].tx=tx;        quad.v[3].ty=ty+600/64;

		return false;
	}


	bool draw()
	{
		// Render graphics
	//	hge.Gfx_BeginScene();
		hge.Gfx_RenderQuad(&quad);
		gui.Render();
		fnt.SetColor(0xFFFFFFFF);
		dfnt.printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d", hge.Timer_GetDelta(), hge.Timer_GetFPS());
		dfnt.RenderVmf();
	//	hge.Gfx_EndScene();

		return false;
	}

	void entry(){ mes("现在进入场景1");}
	void leave(){mes("现在离开场景1");}



}


class myclass2 : hgeSceneObj
{
	ifHge			hge;
	hgeSprite 		spr;
	hgeFontCN		fnt;
 
	this(){
		super("场景2");
		hge= gameInfo.hge;	
		fnt = new hgeFontCN(r"res\font_cn.sbin");
		hgeGUIListbox lb= new hgeGUIListbox(6,400,20,100,100, fnt);
		lb.AddItem("开始");lb.AddItem("设置");lb.AddItem("结束");
		lb.AddItem("123");lb.AddItem("234");lb.AddItem("1235");
		lb.AddItem("我是中国人");

	gui.AddCtrl(cast(hgeGUIObject) lb );
	}
	~this(){
		mes("销毁场景2");
		if(fnt)fnt=null;		// 这里不可使用 delete , 因为指向的可能是引用
	}
	bool update(){
		if(hge.Input_GetKeyState(HGEK_SPACE)){
			this.To("场景1");
		}

		static int lastid =0;
		int id=gui.Update(hge.Timer_GetDelta());
		if(id == -1)
		{
			switch(lastid)
			{
				case 6: gui.Enter();  break;	
				default: 
					break;
			}
		}
		else if(id) { lastid=id; gui.Leave(); }
		return false;
	}
	bool draw(){
		hge.Gfx_Clear(0xff0000ff);
		gui.Render();
		fnt.printf(100,200, HGETEXT_LEFT,"你好,这是中国字!");
		return false;
	}
	void entry(){
		mes("进入场景2");
	}
	void leave(){
		mes("离开场景2");
	}
}


void main() {

	
    gameInfo gi= gameInfo.getInstance();

    gi.setVideo(640,480,16);
    gi.setStr("me","me.log");
    try {
        gi.init();

	hgeSceneObj	sc1= new myclass;
	hgeSceneObj sc2= new myclass2;

		gi.run( sc1);

    } catch (Exception e) {
        mes("主程异常:	",e);
    } 
	
	scope(exit){

		delete gui;
		delete gi;
	}


}

